﻿using System;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Threading;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Renren.Models;
using Renren.Services;
using Renren.ViewModels;

namespace Renren.Pages
{
    public partial class PhotoPage : PhoneApplicationPage
    {
        private PhotosViewModel photosView;

        int leftOffset;
        int rightOffset;
        int startOffset;

        public PhotoPage()
        {
            InitializeComponent();
            photosView = new PhotosViewModel();
            //this.AddProgressIndicator();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            var news = PhoneApplicationService.Current.State["PhotoPage"] as NewsModel;
            PhotoService.GetPhoto(news, photosView);
            DataContext = photosView;
        }

        private void ScrollViewer_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            int finalOffset;
            int frameRate    = 60;
            int minSpeed     = 20;
            var scrollViewer = sender as ScrollViewer;
            int maxOffset    = (int)scrollViewer.ScrollableWidth;
            int curOffset    = (int)scrollViewer.HorizontalOffset;
            int touchShift   = (int)e.TotalManipulation.Translation.X;
            int speed        = (int)e.FinalVelocities.LinearVelocity.X / frameRate;

            if (leftOffset < startOffset && speed < -minSpeed ||
                rightOffset > startOffset && speed > minSpeed)
            {
                return;
            }

            if (touchShift > 0)
            {
                speed = speed < minSpeed ? minSpeed : speed;
                finalOffset = (curOffset / 510) * 510;
            }
            else if (touchShift < 0)
            {
                speed = speed > -minSpeed ? -minSpeed : speed;
                finalOffset = ((curOffset == 0 ? -1 : curOffset) / 510 + 1) * 510;
            }
            else
            {
                finalOffset = curOffset;
            }
            finalOffset = finalOffset <= 0 ? 0 : finalOffset >= maxOffset ? maxOffset : finalOffset;

            scrollViewer.ScrollToHorizontalOffset(curOffset);
            var timer = new DispatcherTimer();
            timer.Tick += (timerSender, timerArgs) =>
            {
                curOffset = (int)scrollViewer.HorizontalOffset - speed;

                if (touchShift > 0 && curOffset > finalOffset ||
                    touchShift < 0 && curOffset < finalOffset)
                {
                    scrollViewer.ScrollToHorizontalOffset(curOffset);
                }
                else
                {
                    scrollViewer.ScrollToHorizontalOffset(finalOffset);
                    timer.Stop();
                }
            };
            timer.Interval = new TimeSpan(1000 * 1000 * 10 / frameRate);
            timer.Start();
        }

        private void ScrollViewer_MouseMove(object sender, MouseEventArgs e)
        {
            var scrollViewer = sender as ScrollViewer;
            int curOffset = (int)scrollViewer.HorizontalOffset;
            if (curOffset < leftOffset)
            {
                leftOffset = curOffset;
            }
            if (curOffset > rightOffset)
            {
                rightOffset = curOffset;
            }
        }

        private void ScrollViewer_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            var scrollViewer = sender as ScrollViewer;
            int curOffset    = (int)scrollViewer.HorizontalOffset;
            leftOffset       = curOffset;
            rightOffset      = curOffset;
            startOffset      = curOffset;
        }
    }
}